package cn.itcast.com.Server;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 郭浩 on 2020/9/7 19:36
 */
public class Request {
    //整个请求报文分为报文，请求的url，请求的方式，协议
    private String requestContent;
    private String requestURL;
    private String method;
    private String protocol;
    //输入流
    private InputStream inputStream;

    //设置请求URL
    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }
    //设置请求的URL
    public void setRequestURL(String requestURL) {
        this.requestURL = requestURL;
    }
    //设置请求的方法
    public void setMethod(String method) {
        this.method = method;
    }
    //设置请求用到协议
    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }
    //获得请求方法
    public String getMethod() {
        return method;
    }
    //获得请求的协议
    public String getProtocol() {
        return protocol;
    }
    //获得输入流
    public InputStream getInputStream() {
        return inputStream;
    }

    //将输入流解析成请求报文
    public void Request(InputStream inputStream) throws IOException {
        //将输入流的东西切分
        this.inputStream = inputStream;
        parse();
    }

    private void parse() throws IOException {
        byte[] bytes = new byte[1024];
        int read = 0;
        try {
            read = inputStream.read(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < read; i++) {
            stringBuffer.append((char) bytes[i]);
        }
        this.requestContent = stringBuffer.toString();
    }

}
